﻿//为grid加载菜单工具条,下面有一个重载方法，此方法不会被调用到
function loadToolbar(rootPath, grid) {
    $.post(rootPath + "/Menu/getButtonList",
        { "opType": "T" },
        function (result) {
            if (!grid.toolbarManager) return;
            if (!result || !result.length) return;
            var items = [];
            //添加服务端的按钮
            var o;
            var method;
            try {
                for (var i = 0, l = result.length; i < l; i++) {
                    o = result[i];
                    method = eval(o.opMethod);
                    items[items.length] = {
                        click: method,
                        text: o.opName,
                        iconClass: o.opClass
                    };
                    items[items.length] = { line: true };
                }
                grid.toolbarManager.set('items', items);
            }
            catch (exception) { 
                alert(exception)
            }
        }, "json");
    }
    //为grid加载菜单工具条(重载方法，增加gridName参数)
function loadToolbar(rootPath, grid, gridName) {
        $.post(rootPath + "/Menu/getButtonList",
        { "opType" : "T","gridName" : gridName },
        function (result) {
            if (!grid.toolbarManager) return;
            if (!result || !result.length) return;
            var items = [];
            //添加服务端的按钮
            var o;
            var method;
            try {
                for (var i = 0, l = result.length; i < l; i++) {
                    o = result[i];
                    method = eval(o.opMethod);
                    items[items.length] = {
                        click: method,
                        text: o.opName,
                        iconClass: o.opClass
                    };
                    items[items.length] = { line: true };
                }
                grid.toolbarManager.set('items', items);
            }
            catch (exception) {
                alert(exception)
            }
        }, "json");
    }
//为grid添加双击事件
function addDoubleClick(rootPath, grid) {
    $.post(rootPath + "/Menu/getButtonList",
        { "opType": "D" },
        function (result) {
            if (!result || !result.length) return;
            //添加服务端的按钮
            try {
                var method = eval(result[0].opMethod);
                grid.set('onDblClickRow', method);
            }
            catch (exception) {
                alert(exception)
            }
        }, "json");
    }

//加载小尺寸工具栏
function loadMinToolbar(rootPath, obj) {
    $.post(rootPath + "/Menu/getMinToolbar",
        { "opType": "M" },
        function (result) {
            if (!result || !result.length) return;
            //添加服务端的按钮
            try {
                var $menuBar = $(result[0]);
                obj.after($menuBar);
            }
            catch (exception) {
                alert(exception)
            }
        }, "json");
    }

    //加载gridColumn
    function loadColumns(rootPath,grid) {
        $.post(rootPath +"/GridColumn/GetColumnByUrl",
        function (gridItems) {
            if (!gridItems || !gridItems.length) return;
            grid.set('columns',gridItems);

        }, "json");
    }
    //加载gridColumn
    function loadColumns(rootPath, grid,gridName) {
        $.post(rootPath + "/GridColumn/GetColumnByUrl",
        { gridName: gridName },
        function (gridItems) {
            if (!gridItems || !gridItems.length) return;
            grid.set('columns', gridItems);

        }, "json");
    }
    //根据form对象构建URL(otherParamsArr:为除form外其它参数，无其它参数是可写为null或[])
    function InitUrl(url, queryForm, otherParamsArr) {
        var query = queryForm.formToArray();
        if (otherParamsArr != null) {
            for (idx = 0; idx < otherParamsArr.length; idx++) {
                query[query.length] = otherParamsArr[idx];
            }
        }
        for (i = 0; i < query.length; i++) {
            if (i == 0) {
                url += "?";
            }
            else {
                url += "&";
            }
            url += escape(query[i]["name"]);
            url += "=";
            url += escape(query[i]["value"]);
        }
        return url;
    }

//右下角提示信息
function showMsg(message) {
    $.messager.show(0, message, 5000);
}

//转换需要的方法
String.prototype.formatDate = function (format) {
    var dateTime = new Date(parseInt(this.substring(6, this.length - 2)));
    return dateTime.format(format);
};

Date.prototype.format = function (format) {
    var o = {
        "M+": this.getMonth() + 1, //month 
        "d+": this.getDate(), //day 
        "h+": this.getHours(), //hour
        "H+": this.getHours(), //hour 
        "m+": this.getMinutes(), //minute 
        "s+": this.getSeconds(), //second 
        "q+": Math.floor((this.getMonth() + 3) / 3), //quarter 
        "S": this.getMilliseconds() //millisecond 
    }

    if (/(y+)/.test(format)) {
        format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    }

    for (var k in o) {
        if (new RegExp("(" + k + ")").test(format)) {
            format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
        }
    }
    return format;
}

Date.prototype.addYears = function (years) {
    this.setFullYear(this.getFullYear() + years);
    return this;
}

Date.prototype.addMonths = function (months) {
    this.setMonth(this.getMonth() + months);
    return this;
}

Date.prototype.addDays = function (days) {
    this.setDate(this.getDate() + days);
    return this;
}

Date.prototype.addHours = function (hours) {
    this.setHours(this.getHours() + hours);
    return this;
}

Date.prototype.addMinutes = function (minutes) {
    this.setMinutes(this.getMinutes() + minutes);
    return this;
}

Date.prototype.addSeconds = function (seconds) {
    this.setSeconds(this.getSeconds() + Seconds);
    return this;
}